package cn.txauction.web.paging;

import cn.txauction.common.AppSetting;

/**
 * @author Huwei
 */
public class PagingWebTool {
	/**
	 * 
	 * @param pageBarsize 页面导航栏显示的页面索引数量
	 * @param currenPage  当前页索引
	 * @param totalPageCount 总页数
	 * @return 记录了应该从第几页显示到第几页的索引值的PageIndex对象
	 */
	public static PageIndex getPageIndex(int pageBarsize, int currenPage, int totalPageCount){
		long startpage = currenPage-(pageBarsize%2==0? pageBarsize/2-1 : pageBarsize/2);
		long endpage = currenPage+pageBarsize/2;
		if(startpage<1){
			startpage = 1;
			if(totalPageCount>=pageBarsize) endpage = pageBarsize;
			else endpage = totalPageCount;
		}
		if(endpage>totalPageCount){
			endpage = totalPageCount;
			if((endpage-pageBarsize)>0) 
				startpage = endpage-pageBarsize+1;
			else startpage = 1;
		}
		return new PageIndex(startpage, endpage);
	}
	
	public static int checkCurrPage(int currpage, int totalpage){
		if(currpage <= 1)
			return 1;
		if(totalpage <= 1)
			return 1;
		if(currpage >= totalpage)
			return totalpage;
		return currpage;
	}
	
	public static PagingParam getPagingParam(int currpage, int totalpage){
		if(currpage <= 1)
			currpage = 1;
		if(totalpage <= 1)
			currpage = 1;
		if(currpage >= totalpage)
			currpage = totalpage;
		int firstResult = (currpage - 1) * AppSetting.pagesize;		
		return new PagingParam(firstResult, AppSetting.pagesize);
	}

}
